package com.soft.springhrms.controller;
import com.soft.springhrms.pojo.Departments;
import com.soft.springhrms.service.DepartmentsService;
import com.soft.springhrms.service.EmployeesService;
import com.soft.springhrms.utils.RedisUtil;
import com.soft.springhrms.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@CrossOrigin
@RequestMapping("/departments")
public class GetDepartmentController {
        @Autowired
        private DepartmentsService departmentsService;
        @Autowired
        private EmployeesService employeeService;
        @Autowired
        private RedisUtil redisUtil;
        @GetMapping
        public List<Departments> getAllDepartments() {
            return departmentsService.getAllDepartments();
        }
    @DeleteMapping("/{id}")
    public ResultVO deleteDepartment(@PathVariable Integer id) {
        int employeeCount = employeeService.countByDepartment(id);
        if (employeeCount > 0) {
            return ResultVO.error(400,"该部门下有员工，无法删除");
        }
        redisUtil.deleteWithDelay("departmentsList");
        boolean result = departmentsService.removeById(id);
        return ResultVO.success(result,"删除成功");
    }
    @PostMapping
    public ResultVO addDepartment(@RequestBody Departments department) {
        redisUtil.deleteWithDelay("departmentsList");
        return ResultVO.success(departmentsService.save(department),"添加部门成功");
    }
    @PutMapping("/{id}")
    public  ResultVO editDepartment(@PathVariable Integer id,@RequestBody Departments department){
        System.out.println(department);
        redisUtil.deleteWithDelay("departmentsList");
        return ResultVO.success(departmentsService.updateById(department),"修改部门成功");
    }
    }

